// 防抖 不断触发高频事件 只能执行最后一次

export const debound = (fn) => {
  let timer = null
  return function () {
    const that = this
    const args = arguments
    clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(that, args)
    }, 500)
  }
}

// 节流 在规定的一段时间内执行一次
export const throttle = (fn, delay) => {
  let timer
  return function () {
    const that = this
    const args = arguments
    if (timer) {
      return
    }
    timer = setTimeout(function () {
      fn.apply(that, args)
      timer = null
    }, delay)
  }
}

// 节流 在规定的一段时间内执行一次 时间戳版
export const throttleNow = (fn, delay) => {
  let previous = 0
  return function () {
    const that = this
    const args = arguments
    const now = new Date()
    console.log(now)
    if (now - previous > delay) {
      fn.apply(that, args)
      previous = now
    }
  }
}
